﻿using M.Warehouse.API.Application.Command.ReservoirsCommand;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.ReservoirsHandlers
{
    /// <summary>
    /// 库区批删中介者
    /// </summary>
    public class ReservoirsBatchDelCommandHandlers : IRequestHandler<ReservoirsBatchDelCommand, int>
    {
        /// <summary>
        /// 库区接口
        /// </summary>
        private readonly IReservoirsWRepository _reservoirsWRepository;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="reservoirsWRepository"></param>
        public ReservoirsBatchDelCommandHandlers(IReservoirsWRepository reservoirsWRepository)
        {
            _reservoirsWRepository = reservoirsWRepository;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(ReservoirsBatchDelCommand request, CancellationToken cancellationToken)
        {
            var list = await _reservoirsWRepository.GetAllAsync();
            var rlist=list.Where(x=>request.ReservoirId.Contains(x.ReservoirId.ToString())).ToList();
            foreach (var r in rlist)
            {
                r.IsDeleted = true;
            }
            return await _reservoirsWRepository.UpdateRange(rlist);
        }
    }
}
